//
//  SettingViewController.swift
//  Exchange
//
//  Created by rui on 2018/4/19.
//  Copyright © 2018年 common. All rights reserved.
//

import UIKit

class SettingViewController: BaseViewController, UITableViewDelegate, UITableViewDataSource{

    private let kSettingTableViewCellID = "kSettingTableViewCellID"
    
    lazy var logoutViewModel =  LogoutViewModel()
    
    var tableViewFooterView: UIButton = {
        let btn = UIButton(type: .custom)
        btn.frame = CGRect(x: 0, y: 0, width: 0, height: 44)
        btn.addTarget(self, action: #selector(logout), for: .touchUpInside)
        btn.setTitle(kText("退出登录"), for: .normal)
        btn.titleLabel?.textAlignment = .center
        btn.titleLabel?.font = UIFont.systemFont(ofSize: 15)
        btn.setTitleColor(UIColor(hexStr: "#ed6868"), for: .normal)
        btn.themeBackground = ThemeManager.ThemeColorKeys.background
        return btn
    }()
    
    lazy var tableView: UITableView = {
        let table = UITableView(frame: self.view.bounds, style: .grouped)
        table.delegate = self
        table.dataSource = self
        table.register(SettingTableViewCell.self, forCellReuseIdentifier: kSettingTableViewCellID)
        if AppInfo.default.isLogin {
            table.tableFooterView = tableViewFooterView
        }
        table.tableHeaderView = UIView(frame: CGRect(x: 0, y: 0, width: 0, height: 0.1))
        table.themeBackground = ThemeManager.ThemeColorKeys.margin
        table.sectionFooterHeight = 10
        table.themeSeparatorColor = ThemeManager.ThemeColorKeys.separator
        table.separatorInset = UIEdgeInsetsMake(0, 16, 0, 16)
        table.sectionHeaderHeight = 0.0001
        return table
    }()

    override func viewDidLoad() {
        super.viewDidLoad()
        self.customTitle = kText("设置")
        initUI()
        initData()
    }
    
    func initData() {
        logoutViewModel.response = {(state,isSuccess,errorMsg)in
            if state == ResponseState.mine_logout {
                if isSuccess {
                   
                }else {
                    
                }
            }
        }
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        self.tableView.reloadData()
    }
    
    func initUI() {
        self.view.addSubview(tableView)
        let titleHeight = self.titleLabel?.frame.size.height ?? 0
        tableView.frame = CGRect(x: 0, y: titleHeight, width: self.view.frame.size.width, height: self.view.height - titleHeight)
    }
    // MARK: - UITableViewDelegate UITableViewDataSource
    
    func numberOfSections(in tableView: UITableView) -> Int {
        return 2
    }
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        if section == 0 {
            return 3
        }
        return 1
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: kSettingTableViewCellID, for: indexPath)
        if indexPath.section == 0 {
            let row = indexPath.row
            if row == 0 {
                cell.textLabel?.text = kText("语言")
                let language = SettingSelectModel.Contant.kLanguages[AppCacheInfo.default.language]
                cell.detailTextLabel?.text = kText(language ?? "")
            }else if  row == 1 {
                cell.textLabel?.text = kText("币种类别")
                let name = SettingSelectModel.Contant.kCurrencys[AppCacheInfo.default.currency]
                cell.detailTextLabel?.text = kText(name ?? "")
            }else if  row == 2 {
                if ThemeManager.default.currentThemeMode() == .night {
                    cell.detailTextLabel?.text = kText("夜间")
                }else {
                    cell.detailTextLabel?.text = kText("白天")
                }
                cell.textLabel?.text = kText("模式")
            }
        }else if indexPath.section == 1 {
            cell.textLabel?.text = kText("关于我们")
            cell.detailTextLabel?.text = ""
        }
        return cell
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 44
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.deselectRow(at: indexPath, animated: false)
        if indexPath.section == 1 {
            self.navigationController?.pushViewController(AboutUsViewController(), animated: true)
        }else if indexPath.section == 0 {
            let row = indexPath.row
            if row == 0 {
                let vc = LanguageOrCurrencyViewController()
                vc.type = .language
                self.navigationController?.pushViewController(vc, animated: true)
            }else if row == 1 {
                let vc = LanguageOrCurrencyViewController()
                vc.type = .currency
                self.navigationController?.pushViewController(vc, animated: true)
            }else if row == 2 {
                popSheet(titles: [kText("白天"),kText("夜间")])
            }
        }
        dPrint("点击")
    }
    ///退出登录
    @objc func logout() {
        dPrint("退出登录")
        CYMBShow.showMessage(kText("退出登录成功"))
        self.navigationController?.popViewController(animated: true)
        logoutViewModel.logout()
    }
    
    func popSheet(titles: [String]) {
        let selectStr = ThemeManager.default.currentThemeMode() == .night ? kText("夜间") : kText("白天")
        SelectActionManger.manager.creatSheet(style: .commonSty, titleArr: titles, selectStr: selectStr, dataSource: self)
    }
    deinit {
        
    }
}

extension SettingViewController: SelectmangerDelegate {
    func selectModel(model:SheetModel) {
        SelectActionManger.manager.close()
        if model.selectStr == kText("夜间") {
            ThemeManager.default.changeTheme(theme: .night)
        }else if model.selectStr == kText("白天") {
            ThemeManager.default.changeTheme(theme: .day)
        }
        self.tableView.reloadData()
    }
}
